PAGE=$.extend(PAGE,{
	"header":function(opt){
		this.settings=$.extend({
			"height":100,
			"id":"header",
			"bgcolor":"cecece",
			"border":false
		},opt);
		this.view=new (Backbone.View.extend({
		    "tagName":"header",
		    "className":"quick-page-header",
		    "template":_.template(PAGE.templates.header()),
		    "render":function(opt){  
		        this.$el.html(this.template(opt));
		        this.$el.css("min-height","50px");
		        return this.$el;
		     }
		}))();
		this.dom=this.view.render(opt);
		this.dom.attr("data-id",this.settings.id).attr("data-role","header");;
		this.dom.height(this.settings.height);
		if(this.dom.find(".adv-search-wrapper").length>0){
			if(this.settings.height>50){
				this.dom.find(".adv-search-wrapper").css("margin-top",((this.settings.height-30)*0.5-15)+"px");
			}
		}
		if("undefined"!=typeof(this.settings.bgcolor)&&false!=this.settings.bgcolor){
			this.dom.css({"background-color":"#"+this.settings.bgcolor});
		}
		if("undefined"!=typeof(this.settings.border)){
			this.dom.css({"border-bottom":"1px solid #"+this.settings.border});
		}
		if("undefined"!=typeof(this.settings.events)&&"undefined"!=typeof(this.settings.events.buttons)){
			var btns=this.settings.events.buttons;
			this.dom.find(".top-right-btn").each(function(){
				var _id=$(this).attr("data-id");
				$(this).off("click");
				if("string"==typeof btns[_id]){
					$(this).on("click",function(fn,_ts){return function(){
						eval(btns[_id]+"(_ts);");
					};}(btns[_id],$(this)));
				}
			});
		}
	}
});
PAGE.header.prototype.setHeight=function(n){
	this.settings.height=n;
	this.dom.height(this.settings.height);
	if(this.dom.find(".adv-search-wrapper").length>0){
	//	this.dom.find(".adv-search-wrapper").height(this.dom.height());
	}
	return this;
}
PAGE.header.prototype.setBorder=function(bd){
	this.settings.border=bd;
	if(this.settings.border&&$.trim(this.settings.bgcolor).length>0){
		this.dom.css({"border-bottom":"1px solid "+this.settings.border});
	}else{
		this.dom.css({"border-bottom":"none"});
	}
	return this;
}
PAGE.header.prototype.setBgcolor=function(bgc){
	this.settings.bgcolor=bgc;
	if(this.settings.bgcolor&&$.trim(this.settings.bgcolor).length>0){
		this.dom.css({"background-color":this.settings.bgcolor});
	}
	return this;
}
PAGE.header.prototype.to=function($wrapper){
	$wrapper.prepend(this.dom);
	return this;
}
PAGE.header.prototype.getDom=function(){
	return this.dom;
}
PAGE.header.prototype.getHeight=function(){
	return this.settings.height;
}
PAGE.header.prototype.getId=function(){
	return this.settings.id;
}
PAGE.header.prototype.click=function(_id,fn){
	var $btn=this.dom.find(".top-right-btn[data-id='"+_id+"']");
	if($btn.length<=0){
		console.log("Header button ["+_id+"] is not found.");
		return this;
	}
	if(typeof fn=="function"){
		$btn.on("click",function(fn,_ts){return function(){
			fn(_ts);
		};}(fn,$btn));
	}else{
		$btn.trigger("click");
	}
	return this;
}
PAGE.templates.header=function(){
	var arr=[];
	arr.push('{if(typeof(logo)!="undefined"){}');
	arr.push('{if(typeof(logo)=="string"){}');
	arr.push('<div class="top-logo" style="background-image:url({-logo});"></div>');
	arr.push('{}else if(typeof(logo)=="object"){}');
	arr.push('<div class="top-logo" style="{if(typeof(logo.width)=="number"){}width:{-logo.width}px;{}}{if(typeof(logo.url)=="string"){}background-image:url({-logo.url});{}}{if(typeof(logo.bgcolor)=="string"||typeof(logo.bgcolor)=="number"){}background-color:#{-logo.bgcolor}{}}" title="{-logo.tip}"></div>');
	arr.push('{}}');
	arr.push('{}}');
	arr.push('{if(typeof(search)!="undefined"){}');
	arr.push('{if(typeof(search)=="string"){}');
	arr.push('<div class="adv-search-btn"></div>');
	arr.push('<div class="adv-search-wrapper">');
	arr.push('<input class="search-txt" placeholder="{-search}" />');
	arr.push('<div class="search-submit"></div>');
	arr.push('</div>');
	arr.push('{}else if(typeof(search)=="object"){}');
	arr.push('{if(typeof(search.advance)!="undefined"&&search.advance){}');
	arr.push('<div class="adv-search-btn"></div>');
	arr.push('{}}');
	arr.push('<div class="adv-search-wrapper">');
	arr.push('<input class="search-txt" placeholder="{-search.label||"Search..."}" />');
	arr.push('<div class="search-submit"></div>');
	arr.push('</div>');
	arr.push('{}}');
	arr.push('{}}');
	arr.push('{if(typeof(buttons)!="undefined"){}');
	arr.push('{for(var i=buttons.length-1;i>=0;i--){var button=buttons[i];}');
	arr.push('<div class="top-right-btn {-button.icon}" title="{-button.text}" data-id="{-button.id}"></div>');
	arr.push('{}}');
	arr.push('{}}');
	return arr.join("");
};
